// 1. express 框架
const express = require("express");
// 2.创建一个后台路由
const router = express.Router();
// 导入用户数据模块
const cartModel = require("../../model/cartModel");

//3.编写接口
//加入购物车
router.get("/joincart", async function (req, res) {
    let { product_id, user_id, product_img, product_name, product_num, product_price } = req.query;
    // console.log(product_id, user_id, product_img, product_name, product_num, product_price);
    await cartModel
        .join_cart(product_id, user_id, product_img, product_name, product_num, product_price)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});
//获取购物车信息
router.get("/getcart", async function (req, res) {
    let { user_id } = req.query;
    // console.log(product_id, user_id, product_img, product_name, product_num, product_price);
    await cartModel
        .get_cart(user_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});
//删除购物车信息
router.get("/deletecart", async function (req, res) {
    let { product_id, user_id } = req.query;
    // console.log(product_id, user_id, product_img, product_name, product_num, product_price);
    await cartModel
        .delete_cart(product_id, user_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});

//添加订单
router.post("/joinorder", async function (req, res) {
    let { user_id, product_id, product_img, product_name, product_price, product_num, user_name, user_phone, user_address } = req.body;
    // console.log(product_id, user_id, product_img, product_name, product_num, product_price);
    await cartModel
        .join_order(user_id, product_id, product_img, product_name, product_price, product_num, user_name, user_phone, user_address)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});
//获取订单信息
router.get("/getorder", async function (req, res) {
    let { user_id } = req.query;
    // console.log(product_id, user_id, product_img, product_name, product_num, product_price);
    await cartModel
        .get_order(user_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});

//获取待收货商品
router.get("/getnotreceived", async function (req, res) {
    let { user_id } = req.query;
    console.log(user_id);
    // console.log(product_id, user_id, product_img, product_name, product_num, product_price);
    await cartModel
        .get_not_received(user_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});
//获取已收货商品
router.get("/gettoreceived", async function (req, res) {
    let { user_id } = req.query;
    console.log(user_id);
    // console.log(product_id, user_id, product_img, product_name, product_num, product_price);
    await cartModel
        .get_to_received(user_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});
//修改待收货商品为已收货
router.get("/modifyreceivedstate", async function (req, res) {
    let { user_id, product_id } = req.query;
    console.log(user_id);
    // console.log(product_id, user_id, product_img, product_name, product_num, product_price);
    await cartModel
        .modify_received_state(user_id, product_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});

//添加地址
router.post("/joinaddress", async function (req, res) {
    let { user_id, user_name, user_phone, user_address } = req.body;
    await cartModel
        .join_address(user_id, user_name, user_phone, user_address)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});

//获取地址列表
router.get("/getaddress", async function (req, res) {
    let { user_id } = req.query;
    await cartModel
        .get_address(user_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});

//判断是否收藏
router.get("/getiscollect", async function (req, res) {
    let { user_id, product_id } = req.query;
    await cartModel
        .get_collect(user_id, product_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});
//添加收藏
router.post("/joincollect", async function (req, res) {
    let { user_id, product_id, product_img, product_name, product_price } = req.body;
    await cartModel
        .add_collect(user_id, product_id, product_img, product_name, product_price)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});
//取消收藏
router.get("/deletecollect", async function (req, res) {
    let { product_id, user_id } = req.query;
    await cartModel
        .delete_collect(product_id, user_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});
//获取收藏列表
router.get("/getcollectlist", async function (req, res) {
    let { user_id } = req.query;
    await cartModel
        .get_collectlist(user_id)
        .then((content) => {
            res.send(content);
        })
        .catch((err) => {
            res.send(err);
        });
});

// 4.把当前路由暴露在模块系统下
module.exports = router;
